package com.campus.food.controller;

import com.campus.food.model.entity.Banner;
import com.campus.food.service.BannerService;
import com.campus.food.util.Result;
import com.campus.food.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 轮播图控制器
 * 处理轮播图相关的HTTP请求
 * 
 * @author Claude3.7 Sonnet
 * @since 2025-06-12
 */
@RestController
@RequestMapping("/api/banners")
@Validated
public class BannerController {
    
    @Autowired
    private BannerService bannerService;
    
    /**
     * 添加轮播图（管理员专用）
     * POST /api/banners
     */
    @PostMapping
    public Result<Banner> addBanner(@Valid @RequestBody Banner banner) {
        try {
            Banner savedBanner = bannerService.addBanner(banner);
            return Result.success("轮播图添加成功", savedBanner);
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("添加轮播图失败，请稍后重试");
        }
    }
    
    /**
     * 根据ID获取轮播图信息
     * GET /api/banners/{id}
     */
    @GetMapping("/{id}")
    public Result<Banner> getBannerById(@PathVariable @NotNull Long id) {
        try {
            Banner banner = bannerService.getBannerById(id);
            return Result.success(banner);
        } catch (BusinessException e) {
            return Result.notFound(e.getMessage());
        } catch (Exception e) {
            return Result.error("获取轮播图信息失败");
        }
    }
    
    /**
     * 更新轮播图信息（管理员专用）
     * PUT /api/banners/{id}
     */
    @PutMapping("/{id}")
    public Result<String> updateBanner(@PathVariable @NotNull Long id,
                                     @RequestBody Banner banner) {
        try {
            banner.setId(id);
            boolean success = bannerService.updateBanner(banner);
            if (success) {
                return Result.success("轮播图更新成功");
            } else {
                return Result.error("轮播图更新失败");
            }
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("更新轮播图失败");
        }
    }
    
    /**
     * 删除轮播图（管理员专用）
     * DELETE /api/banners/{id}
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteBanner(@PathVariable @NotNull Long id) {
        try {
            boolean success = bannerService.deleteBanner(id);
            if (success) {
                return Result.success("轮播图删除成功");
            } else {
                return Result.error("轮播图删除失败");
            }
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("删除轮播图失败");
        }
    }
    
    /**
     * 获取所有轮播图列表
     * GET /api/banners
     */
    @GetMapping
    public Result<List<Banner>> getAllBanners() {
        try {
            List<Banner> banners = bannerService.getAllBanners();
            return Result.success(banners);
        } catch (Exception e) {
            return Result.error("获取轮播图列表失败");
        }
    }
    
    /**
     * 获取活跃状态的轮播图列表（用于前端展示）
     * GET /api/banners/active
     */
    @GetMapping("/active")
    public Result<List<Banner>> getActiveBanners() {
        try {
            List<Banner> banners = bannerService.getActiveBanners();
            return Result.success(banners);
        } catch (Exception e) {
            return Result.error("获取轮播图列表失败");
        }
    }
    
    /**
     * 更新轮播图状态（管理员专用）
     * PUT /api/banners/{id}/status
     */
    @PutMapping("/{id}/status")
    public Result<String> updateBannerStatus(@PathVariable @NotNull Long id,
                                           @RequestParam @NotNull Integer status) {
        try {
            boolean success = bannerService.updateBannerStatus(id, status);
            if (success) {
                String message = status == 1 ? "轮播图已启用" : "轮播图已禁用";
                return Result.success(message);
            } else {
                return Result.error("状态更新失败");
            }
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("更新轮播图状态失败");
        }
    }
    
    /**
     * 获取轮播图列表（分页，管理员专用）
     * GET /api/banners/page
     */
    @GetMapping("/page")
    public Result<List<Banner>> getBannerList(@RequestParam(defaultValue = "1") int page,
                                            @RequestParam(defaultValue = "10") int size,
                                            @RequestParam(required = false) Integer status) {
        try {
            List<Banner> banners = bannerService.getBannerList(page, size, status);
            long total = bannerService.getBannerCount(status);
            return Result.success(banners, total);
        } catch (Exception e) {
            return Result.error("获取轮播图列表失败");
        }
    }
    
    /**
     * 批量删除轮播图（管理员专用）
     * DELETE /api/banners/batch
     */
    @DeleteMapping("/batch")
    public Result<String> deleteBannerBatch(@RequestParam List<Long> ids) {
        try {
            boolean success = bannerService.deleteBannerBatch(ids);
            if (success) {
                return Result.success("批量删除成功");
            } else {
                return Result.error("批量删除失败");
            }
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("批量删除轮播图失败");
        }
    }
    
    /**
     * 更新轮播图排序（管理员专用）
     * PUT /api/banners/{id}/sort
     */
    @PutMapping("/{id}/sort")
    public Result<String> updateBannerSort(@PathVariable @NotNull Long id,
                                         @RequestParam @NotNull Integer sort) {
        try {
            boolean success = bannerService.updateBannerSort(id, sort);
            if (success) {
                return Result.success("排序更新成功");
            } else {
                return Result.error("排序更新失败");
            }
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("更新轮播图排序失败");
        }
    }
} 